<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-title">美味外卖</div>
      <div class="login-subtitle">{{ roleTexts[activeRole] }}登录</div>
      
      <el-tabs v-model="activeRole" @tab-click="handleRoleChange">
        <el-tab-pane label="用户" name="user"></el-tab-pane>
        <el-tab-pane label="商家" name="merchant"></el-tab-pane>
        <el-tab-pane label="骑手" name="rider"></el-tab-pane>
      </el-tabs>
      
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-button" @click="handleLogin" :loading="loading">登录</el-button>
        </el-form-item>
        <div class="login-options">
          <span class="register-link" @click="$router.push('/register')">注册账号</span>
          <span class="forget-password">忘记密码</span>
        </div>
      </el-form>
      
      <div class="admin-login-link">
        <router-link to="/admin/login">管理员登录</router-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      activeRole: 'user',
      roleTexts: {
        user: '用户',
        merchant: '商家',
        rider: '骑手'
      },
      roleUrls: {
        user: '/login',
        merchant: '/merchant/login',
        rider: '/rider/login'
      },
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    handleRoleChange() {
      this.$refs.loginForm.resetFields()
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$http.post(this.roleUrls[this.activeRole], this.loginForm)
            .then(res => {
              if (res.data.code === 200) {
                this.$message.success('登录成功')
                // 保存用户信息及token
                const userInfo = res.data.data
                userInfo.role = this.activeRole
                this.$store.dispatch('login', userInfo)
                
                // 跳转到对应页面
                const redirectPath = this.redirect || (this.activeRole === 'user' ? '/' : `/${this.activeRole}`)
                this.$router.push(redirectPath)
              } else {
                this.$message.error(res.data.message || '登录失败')
              }
            })
            .catch(err => {
              console.error(err)
              this.$message.error('登录失败，请稍后再试')
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}
.login-box {
  width: 400px;
  padding: 40px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.login-title {
  font-size: 28px;
  font-weight: bold;
  text-align: center;
  color: #409EFF;
  margin-bottom: 10px;
}
.login-subtitle {
  font-size: 18px;
  text-align: center;
  color: #666;
  margin-bottom: 20px;
}
.login-form {
  margin-top: 30px;
}
.login-button {
  width: 100%;
}
.login-options {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
  color: #409EFF;
  font-size: 14px;
}
.register-link, .forget-password {
  cursor: pointer;
}
.admin-login-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
}
.admin-login-link a {
  color: #909399;
  text-decoration: none;
}
.admin-login-link a:hover {
  color: #409EFF;
  text-decoration: underline;
}
</style> 